package com.wiloon.java8x.database;

import com.alibaba.druid.pool.DruidDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.PreparedStatement;

/**
 * Created by wiloon on 1/17/16.
 */
public class DruidInsert {
    private static Logger logger = LoggerFactory.getLogger(JdbcInsert.class);
    public static void main(String[] args) {
        String mySqlUrl="jdbc:mysql://127.0.0.1:3306/test";
        String mySqlUserName="user0";
        String mySqlPassword="password0";
        String sql ="insert into test_insert (name,email) values('nameStr','emailStr')";
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setUrl(mySqlUrl);
        druidDataSource.setUsername(mySqlUserName);
        druidDataSource.setPassword(mySqlUserName);
        druidDataSource.setDriverClassName("com.mysql.jdbc.Driver");
        int count = 1000 ;
        int commitPoint=100;
        try {
            Connection conn = druidDataSource.getConnection();
            PreparedStatement prest= conn.prepareStatement(sql);



            long a=System.currentTimeMillis();
            for(int x = 1; x <= count; x++){

                // prest.execute();
                prest.executeUpdate();
                if(x%commitPoint==0){
                    conn.commit();
                }
            }
            long b=System.currentTimeMillis();
            logger.debug("count:{}",count);
            logger.debug("start:{}",a);
            logger.debug("end:{}",b);
            logger.debug("b-a:{}",b-a);
            logger.debug("qps:{}",count/((b-a)/1000));
            conn.close();
        } catch (Exception ex) {

        }



    }
}
